﻿#region Using

using System;
using System.Data;
using System.Web.UI;
using AjaxPro;
using RuleCode.Enum;
using RuleCode.Table;

#endregion

namespace map.member
{
    public partial class regist1 : Page
    {
        private string area4id;
        private string ditulat;
        private string ditulong;
        private bool isRedirect;

        protected void Page_Load(object sender, EventArgs e)
        {
            Literal_map.Text = RuleCode.Util.getmapurl();
            Utility.RegisterTypeForAjax(typeof (AjaxFunction));
            InitMap();

            //
            if (!IsPostBack)
            {
                //处理从其它页面跳转注册的参数,格式area4id=764246&long=116.514129638672&lat=39.913423004887 
                if (Request.Params.HasKeys())
                {
                    isRedirect = true;
                    if (!string.IsNullOrEmpty(Request.Params["area4id"]))
                    {
                        area4id = Request.Params["area4id"];
                    }
                    if (!string.IsNullOrEmpty(Request.Params["long"]))
                    {
                        ditulong = Request.Params["long"];
                    }
                    if (!string.IsNullOrEmpty(Request.Params["lat"]))
                    {
                        ditulat = Request.Params["lat"];
                    }
                }
                Bind0();
            }
        }

        private void InitMap()
        {
            string ScriptTag = "";


            ScriptTag =
                "<script type=\"text/javascript\">markpos(" + "116.39679908752441" + ","
                + "" + "39.917043746896674,'" + "2222" + "');</script>";

            ClientScript.RegisterStartupScript(GetType(), "bodyevent", ScriptTag);
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)

                //if (Session["check"] == null)
                //    //if (1==1)
                //{
                //    Image1.ImageUrl = "~/common/createImage.aspx";
                //    showCaptchaMess.Text = "输入新验证码!";
                //}
                //else
                //{
                //    if (checkText.Text.ToString() == Session["check"].ToString())

            {
                //
                //检查用户名邮箱
                string mailAddress = TextBox1.Text;
                cbuser u1 = cbuser.Find("cbusernickname = '" + TextBox2.Text + "' ");
                if (u1.Count > 0)
                {
                    showUserError.Text = "用户名不可用！";
                }
                else
                {
                    cbuser u = cbuser.Find("cbuserid = '" + mailAddress + "' ");
                    if (u.Count > 0)
                    {
                        showMailMessage.Text = "该邮箱已存在！！";
                    }
                    else
                    {
                        cbuser user = new cbuser();
                        user.Init(0);
                        user.New();
                        user.cbuserid = TextBox1.Text;
                        user.cbusernickname = TextBox2.Text;
                        user.cbpassword = TextBox4.Text;
                        user.cbsex = RadioButtonList1.SelectedValue;
                        user.JuniorSchoolName = TextBox5.Text;
                        user.JuniorSchoolGraduateYear.StringValue = DropDownList1.SelectedValue;
                        user.SeniorSchoolName = TextBox6.Text;
                        user.SeniorSchoolGraduateYear.StringValue = DropDownList2.SelectedValue;
                        user.ColleageName = TextBox7.Text;
                        user.ColleageGraduateYear.StringValue = DropDownList3.SelectedValue;
                        user.LastEmployeeName = TextBox8.Text;
                        user.taobaolong.StringValue = mapLng.Text;
                        user.taobaolat.StringValue = MapLat.Text;
                        user.省编码 = zfunction.Province;
                        user.县编码 = DropDownList4.SelectedValue;
                        user.区编码 = DropDownList5.SelectedValue;
                        user.区域编码 = DropDownList6.SelectedValue;
                        user.cbaddress = TextBox19.Text;
                        user.BokerStatus = BrokerStatus.Common;
                        //少了一个字段?yujie.zhou begin
                        user.cbUserBizArea.Value = DropDownList7.SelectedValue;
                        //end

                        //add bu dingyiliang
                        user.lastChatInfoId.Value = "0";
                        //end add by dingyuliang

                        user.Insert();
                        cbuser.login(user.cbuserid.ToString(), user.cbpassword.ToString());
                        cbuser.afterinsert(user);


                        if (String.IsNullOrEmpty(Request.Params["redirecturl"].ToString()))
                        {
                            //Response.Redirect(ResolveUrl("~/default.aspx"));

                            //提示成功，返回登录
                            //string ScriptTag =
                            //    "<script type=\"text/javascript\">setTimeout(\"showDianlog()\",500);</script>"
                            //    ;

                            // ClientScript.RegisterStartupScript(GetType(), "bodyevent", ScriptTag);
                            //ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Show", ScriptTag, false);
                            ////Response.Redirect("login.aspx");
                        }
                        else
                        {
                            //Response.Redirect(Request.Params["redirecturl"].ToString());
                        }
                        BizArea x = BizArea.Find(" 区编码 = '" + DropDownList5.Text + "' "
                                     + " and 县编码 = '" + DropDownList4.Text + "' " +
                                     " and 区域编码 = '" + DropDownList6.Text + "' " +
                                     " and BizAreaName = '" + DropDownList7.Text + "' ");
                        Response.Redirect(ResolveUrl("~/search/areadetail.aspx?id=" + x.RECID));
                        //提示成功，返回登录
                        //string ScriptTag =
                        //    "<script type=\"text/javascript\">setTimeout(\"showDianlog()\",500);</script>"
                        //    ;

                        //ClientScript.RegisterStartupScript(GetType(), "bodyevent", ScriptTag);
                        //ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Show", ScriptTag, false);
                        //Response.Redirect("login.aspx");
                    }
                }
            }
            //}
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //CountyBind();
            Bind1();
        }

        private void CountyBind()
        {
            string xian = DropDownList4.Text;
            区 x = 区.Find(" 县编码 = '" + xian + "' ");
            DropDownList5.Items.Clear();
            DropDownList6.Items.Clear();
            DropDownList7.Items.Clear();
            foreach (DataRow item in x.MyTable.Rows)
            {
                DropDownList5.Items.Add(item["区编码"].ToString());
            }
            string ScriptTag = "";
            if (!IsPostBack)
            {
                if (x.taobaolong.Value == 0 || x.taobaolat.Value == 0)
                {
                    //ScriptTag =
                    //    "<script type=\"text/javascript\">markpos2(" + "116.396" + ","
                    //    + "" + "39.917,'" + x.区编码.ToString() + "');</script>";
                    ScriptTag =
                        "<script type=\"text/javascript\">markpos2(" + "116.39679908752441" + ","
                        + "" + "39.917043746896674,'" + x.区编码.ToString() + "');</script>";
                }
                else
                {
                    ScriptTag =
                        "<script type=\"text/javascript\">markpos2(" + x.taobaolat.ToString() + ","
                        + x.taobaolong.ToString() + ",'" + x.区编码.ToString() + "');</script>";
                }
                //ClientScript.RegisterStartupScript(GetType(), "bodyevent", ScriptTag);
            }
            else
            {
                //改变了值的情况、
                ScriptTag =
                    "<script type=\"text/javascript\">markpos2(" + x.taobaolat.ToString() + ","
                    + x.taobaolong.ToString() + ",'" + x.区编码.ToString() + "');</script>";
            }
            ClientScript.RegisterStartupScript(GetType(), "bodyevent1", ScriptTag);
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            Bind2();
        }

        protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
        {
            Bind3();
        }

        protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)
        {
            Bind4();
        }

        /// <summary>
        /// 绑定省
        /// </summary>
        private void Bind0()
        {
            string sheng = zfunction.Province;
            县 x = 县.Find(" 省编码 = '" + sheng + "' ");
            DropDownList4.Items.Clear();
            DropDownList5.Items.Clear();
            DropDownList6.Items.Clear();
            DropDownList7.Items.Clear();
            foreach (DataRow item in x.MyTable.Rows)
            {
                DropDownList4.Items.Add(item["县编码"].ToString());
            }

            if (x.Count > 0)
            {
                Bind1();
            }
        }

        /// <summary>
        /// 绑定县
        /// </summary>
        private void Bind1()
        {
            string xian = DropDownList4.Text;
            区 q = 区.Find(" 县编码 = '" + xian + "' ");
            DropDownList5.Items.Clear();
            DropDownList6.Items.Clear();
            DropDownList7.Items.Clear();
            foreach (DataRow item in q.MyTable.Rows)
            {
                DropDownList5.Items.Add(item["区编码"].ToString());
            }
            if (q.Count > 0)
            {
                Bind2();
            }
            else
            {
                //todo:用geocode显示该县
                string provice = zfunction.Province;
                string county = DropDownList4.SelectedValue;
                string query = provice + "," + county;
                string scriptTag = "showAddress('" + query + "');";
                ScriptManager.RegisterStartupScript(
                    UpdatePanel2, UpdatePanel2.GetType(), "bodyevent3", scriptTag, true);
            }
        }

        /// <summary>
        /// 绑定区,镇,朝阳区
        /// </summary>
        private void Bind2()
        {
            区域 q = 区域.Find(" 区编码 = '" + DropDownList5.Text + "' "
                           + " and 县编码 = '" + DropDownList4.Text + "' ");
            DropDownList6.Items.Clear();
            DropDownList7.Items.Clear();
            foreach (DataRow item in q.MyTable.Rows)
            {
                DropDownList6.Items.Add(item["区域编码"].ToString());
            }
            if (q.Count > 0)
            {
                Bind3();
            }
            else
            {
                区 q1 = 区.Find(" 区编码 = '" + DropDownList5.Text + "' ");
                markpos2(q1.taobaolat.ToString(), q1.taobaolong.ToString(), q1.区编码.ToString());
            }
        }

        /// <summary>
        /// 小区域,呼家楼
        /// </summary>
        private void Bind3()
        {
            int row = BindBizarea();

            if (!isRedirect && row > 0)
            {
                Bind4();
            }
            else
            {
                if (isRedirect)
                {
                    区域 q = 区域.Find(" recid = '" + area4id + "' ");
                    DropDownList4.SelectedValue = q.县编码.ToString();
                    区 qu = 区.Find(" 县编码 = '" + DropDownList4.SelectedValue + "' ");
                    DropDownList5.DataSource = qu.MyTable;
                    DropDownList5.DataTextField = "区编码";
                    DropDownList5.DataValueField = "区编码";
                    DropDownList5.DataBind();
                    DropDownList5.SelectedValue = q.区编码.ToString();
                    DropDownList6.Items.Clear();
                    ;
                    区域 quyu =
                        区域.Find(" 县编码 = '" + DropDownList4.SelectedValue + "' and 区编码 = '" + DropDownList5.SelectedValue +
                                " '");
                    DropDownList6.DataSource = quyu.MyTable;
                    DropDownList6.DataTextField = "区域编码";
                    DropDownList6.DataValueField = "区域编码";
                    DropDownList6.DataBind();
                    DropDownList6.SelectedValue = q.区域编码.ToString();
                    if (row > 0)
                    {
                        int row1 = BindBizarea();
                        if (row1 > 0)
                        {
                            Bind4();
                        }
                    }
                    else
                    {
                        markpos2(q.taobaolat.ToString(), q.taobaolong.ToString(), q.区域编码.ToString());
                    }
                }
                else
                {
                    if (row > 0)
                    {
                        Bind4();
                    }

                    区域 q = 区域.Find(" 区编码 = '" + DropDownList5.Text + "' "
                                   + " and 县编码 = '" + DropDownList4.Text + "' " + " and 区域编码 = '" + DropDownList6.Text +
                                   "' ");
                    markpos2(q.taobaolat.ToString(), q.taobaolong.ToString(), q.区域编码.ToString());
                }
            }
        }

        private int BindBizarea()
        {
            BizArea x = BizArea.Find(" 区编码 = '" + DropDownList5.Text + "' "
                                     + " and 县编码 = '" + DropDownList4.Text + "' " + " and 区域编码 = '" + DropDownList6.Text +
                                     "' ");
            DropDownList7.Items.Clear();
            foreach (DataRow item in x.MyTable.Rows)
            {
                DropDownList7.Items.Add(item["BizAreaName"].ToString());
            }
            return x.Count;
        }

        /// <summary>
        /// 大厦,商城,小区等,京广中心
        /// </summary>
        private void Bind4()
        {
            //显示所选地标的位置
            BizArea x = BizArea.Find(" 区编码 = '" + DropDownList5.Text + "' "
                                     + " and 县编码 = '" + DropDownList4.Text + "' " +
                                     " and 区域编码 = '" + DropDownList6.Text + "' " +
                                     " and BizAreaName = '" + DropDownList7.Text + "' ");
            markpos2(x.taobaolat.ToString(), x.taobaolong.ToString(), x.BizAreaName.ToString());
        }

        private void markpos2(string taobaolat, string taobaolong, string name)
        {
            string ScriptTag = "";
            if (!IsPostBack)
            {
                if (taobaolong == "0" || taobaolat == "0")
                {
                    ScriptTag =
                        "<script type=\"text/javascript\">markpos2(" + "116.39679908752441" + ","
                        + "" + "39.917043746896674,'" + name + "');</script>";
                }
                else
                {
                    ScriptTag =
                        "<script type=\"text/javascript\">markpos2(" + taobaolong + ","
                        + taobaolat + ",'" + name + "');</script>";
                }
                ScriptManager.RegisterStartupScript(Page, Page.GetType(),
                                                    "messagew", ScriptTag, false);
            }
            else
            {
                //改变了值的情况、
                ScriptTag = "markpos2(" + taobaolong + ","
                            + taobaolat + ",'" + name + "');";

                ScriptManager.RegisterStartupScript(
                    UpdatePanel2, UpdatePanel2.GetType(), "bodyevent3", ScriptTag, true);
            }
        }

        protected void TextBox7_TextChanged(object sender, EventArgs e)
        {
        }
    }
}